<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" :width="700">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="70px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item class="" label="订单编号" prop="id">
            <el-input v-model="formData.id" disabled placeholder="请输入"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单日期" prop="orderDateStr ">
          <el-date-picker
            v-model="formData.orderDateStr "
            value-format="YYYY-MM-DD"
            type="date"
            start-placeholder="订单日期"
            disabled
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="到货点" prop="arrivalPoint">
          <el-input
            v-model="formData.arrivalPoint"
            placeholder="到货点"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出货方式" prop="deliveryMode">
          <el-input
            v-model="formData.deliveryMode"
            placeholder="出货方式"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="厂长" prop="manager">
          <el-input
            v-model="formData.manager"
            placeholder="厂长"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开发业务" prop="businessName">
          <el-input
            v-model="formData.businessName"
            placeholder="开发业务"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="业务" prop="operator">
          <el-input
            v-model="formData.operator"
            placeholder="业务"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="龙亿编号" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="龙亿编号"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="订单总数" prop="totalNum">
          <el-input
            v-model="formData.totalNum"
            placeholder="备注"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            placeholder="备注"
            clearable
            class="!w-240px"
          />
        </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="submitForm" ref="subBtn">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
  import * as orderApi from '@/api/hcxly/order/Index'
  import { FormRules } from 'element-plus'

  defineOptions({ name: 'orderEdit' });

  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
    id: '',
    totalNum: '',
    orderDateStr: '',
    orderDate : 0,
    arrivalPoint: '',
    deliveryMode: '',
    manager: '',
    businessName: '',
    operator: '',
    code: '',
    remark: ''
  })

  //打开弹窗
  const open = async (row) => {
    console.log(row)
    dialogVisible.value = true
    dialogTitle.value = t('修改')
    formData.value.id = row.id
    formData.value.totalNum = row.totalNum
    formData.value.orderDateStr = row.orderDateStr
    formData.value.orderDate = row.orderDate
    console.log(formData.value.orderDate)
    formData.value.arrivalPoint = row.arrivalPoint
    formData.value.deliveryMode = row.deliveryMode
    formData.value.manager = row.manager
    formData.value.businessName = row.businessName
    formData.value.operator = row.operator
    formData.value.code = row.code
    formData.value.remark = row.remark
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  //表单验证
  const formRules = reactive<FormRules>({
  })

  const formRef = ref() // 表单 Ref
  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {

    await message.confirm('修改会造成部分数据被清楚，确认修改?')


    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value
      console.log(data)
      await orderApi.editOrder(data)
      message.success(t('修改成功'))
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')

      // resetForm()
    } finally {
      formLoading.value = false
    }
  }

</script>
